#!/usr/bin/env python
import os
import sys
import time
import threading
from socket import *
import json   #json模块导入 

def socketMethod():
    print("Run socket")
    s = socket(AF_INET, SOCK_STREAM)
    s.connect(('192.168.1.172',8899))
    #s.connect(('169.254.137.138',8123))
    while True:
        time.sleep( 10 )
        data = '{"msg": "quest_dt_info"}\0'
        if not data:
            break
        s.send(bytes(data, 'utf-8'))
        data = s.recv(1024)
        if not data:
            break
        print(data)
        #if 'q'== input('>'):
        #   break;
    s.close()

''' 
    while True:  # 这里要一直开启监听socket
        c, addr = s.accept()
        print(addr)
        c.send("OK")
        c.close()
'''
#socketMethod()
'''
  功能流程：
  互斥锁 
  解析json 格式字符接口  
  界面数据和数据库数据同步 

'''
def pars_json(jsonData):
    text = json.loads(jsonData)
    pass
    #return text.keys(), text.value()


def Data_Synchronism():
    
    pass


if __name__ == "__main__":
    #t = threading.Thread(target=socketMethod, name='socketMethod')  # 线程对象.
    #t.start()

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "IvAutoServices.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise
    execute_from_command_line(sys.argv)



